package com.human.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.Attendance;
import com.human.server.entity.Salary;
import com.human.server.model.R;
import com.human.server.model.request.AttendanceRequest;
import com.human.server.model.request.SalaryRequest;
import com.human.server.service.impl.SalaryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/human/salary")
public class SalaryController {

    @Autowired
    private SalaryServiceImpl salaryService;

    /**
     * 分页查询
     * @param salaryRequest
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody SalaryRequest salaryRequest) {
        return R.success(salaryService.page(salaryRequest,
                new LambdaQueryWrapper<Salary>()
                        .eq(StrUtil.isNotBlank(salaryRequest.getMonth()), Salary::getMonth, salaryRequest.getMonth())
                        .like(StrUtil.isNotBlank(salaryRequest.getEmployeeName()), Salary::getEmployeeName, salaryRequest.getEmployeeName())
                        .orderByDesc(Salary::getMonth)));
    }

    /**
     * 新增
     * @param salary
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Salary salary) {
        return R.success(salaryService.save(salary));
    }

    /**
     * 修改
     * @param salary
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody Salary salary) {
        return R.success(salaryService.updateById(salary));
    }

    /**
     * 删除
     * @param salary
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Salary salary) {
        return R.success(salaryService.removeById(salary.getId()));
    }

    /**
     * 详情
     * @param salary
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody Salary salary) {
        return R.success(salaryService.getById(salary.getId()));
    }

    /**
     * 列表
     * @return
     */
    @PostMapping("/list")
    public R list() {
        return R.success(salaryService.list());
    }

    /**
     * upload
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        try {
            salaryService.upload(file);
        } catch (IOException e) {
            return R.fail("上传失败");
        }
        return R.success();
    }
}
